package com.quanyan.stadium.service.impl;

import com.github.ltsopensource.core.json.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.common.http.HttpClientUtils;
import com.quanyan.common.utils.GsonUtils;
import com.quanyan.stadium.service.StadiumSmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: zyq
 * Date: 2017/2/17 0017
 * Time: 10:25
 */
@Service("clubSmsService")
public class StadiumSmsServiceImpl implements StadiumSmsService {

        private Logger logger = LoggerFactory.getLogger(StadiumSmsServiceImpl.class);

        @Value("${c.sendSmsApi}")
        private String sendSmsApi;

        @Value("${c.checkCodeApi}")
        private String checkCodeApi;

        @Value("${c.address}")
        private String address;

        /**
         * 短信下发
         *
         * @param mobiles
         * @param templateId
         * @param type
         * @param params
         * @return
         */
        @Override
        public APIResponse sendSms(List<String> mobiles, int templateId, int type, List<String> params) {
            Map headMap = new HashMap<>();
            headMap.put("userCookiesName", "");
            Map paramMap = new HashMap();
            paramMap.put("ver", "");
            paramMap.put("protocol_ver", 1);
            paramMap.put("mobiles", mobiles);
            paramMap.put("template_id", templateId);
            paramMap.put("type", type);
            paramMap.put("params", params);
            APIResponse response = null;
            String result = null;
            try {
                logger.warn("--发送短信手机号--" + JSON.toJSONString(mobiles) + "----参数--" + JSON.toJSONString(params));
                result = HttpClientUtils.postJson(address + sendSmsApi, GsonUtils.toJson(paramMap), headMap);
                logger.warn("--短信返回结果--" + result);
                response = GsonUtils.jsonStrToEntity(result, APIResponse.class);
            } catch (Exception e) {
                logger.info("请求返回信息：{}，异常信息：{}", result, e);
            }
            return response;
        }

        /**
         * 发送验证码
         *
         * @param mobile
         * @return
         */
        @Override
        public APIResponse sendSms(String mobile) {
            return sendSms(Arrays.asList(mobile), CODE_TYPE, CLUB_MESSAGE_TEMPLATE_CODE_NOTICE, new ArrayList<String>());
        }

        /**
         * 发送通知短信
         *
         * @param mobile
         * @param templateId
         * @param params
         * @return
         */
        @Override
        public APIResponse sendSms(String mobile, int templateId, List<String> params) {
            return sendSms(Arrays.asList(mobile), templateId, NOTICE_TYPE, params);
        }

        /**
         * 验证码校验
         *
         * @param validCode 验证码
         * @return
         */
        @Override
        public APIResponse checkCode(String validCode, String mobile) {
            Map headMap = new HashMap<>();
            headMap.put("userCookiesName", "");
            Map paramMap = new HashMap();
            paramMap.put("ver", "");
            paramMap.put("protocol_ver", 1);
            paramMap.put("verifycode", validCode);
            paramMap.put("mobile", mobile);
            APIResponse response = null;
            String result = null;
            try {
                result = HttpClientUtils.postJson(address + checkCodeApi, GsonUtils.toJson(paramMap), headMap);
                response = GsonUtils.jsonStrToEntity(result, APIResponse.class);
            } catch (Exception e) {
                logger.info("请求返回信息：{}，异常信息：{}", result, e);
            }
            return response;
        }

}
